<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>nginx 不记录指定文件类型的日志 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>nginx 不记录指定文件类型的日志</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LNMP/">LNMP</a> <a class="article__tag-link" href="/tags/Nginx/">Nginx</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先看看 nginx 日志相关配置</p>
<h2 id="1-日志格式"><a href="#1-日志格式" class="headerlink" title="1.日志格式"></a>1.日志格式</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 nginx.conf 中定义日志格式</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># vim /usr/local/nginx/conf/nginx.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">log_format combined_realip <span class="string">'$remote_addr $http_x_forwarded_for [$time_local]'</span></div><div class="line"><span class="string">'$host "$request_uri" $status'</span></div><div class="line"><span class="string">'"$http_referer" "$http_user_agent"'</span>;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：remote_addr 表示远程的 ip ；http_x_forwarded_for 表示代理的 ip ；time_local 表示时间；host 表示域名；request_uri  表示访问的地址；status 表示状态码；http_referer 表示referer ； http_user_agent 表示user_agent用户代理；</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个格式该怎么去使用，在虚拟主机配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># vim /usr/local/nginx/conf/vhosts/123.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">access_log /tmp/access.log combined_realip;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nginx%20%E4%B8%8D%E8%AE%B0%E5%BD%95%E6%8C%87%E5%AE%9A%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%97%A5%E5%BF%97/01.png?raw=true" alt=""></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># service nginx configtest</span></div><div class="line">[root@lnmp ~]<span class="comment"># service nginx reload</span></div><div class="line">[root@lnmp ~]<span class="comment"># curl -x127.0.0.1:80 www.123.com/1234 -I</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样 /tmp 下就会生成 access.log </p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nginx%20%E4%B8%8D%E8%AE%B0%E5%BD%95%E6%8C%87%E5%AE%9A%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%97%A5%E5%BF%97/02.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面的日志格式，会记录代理的 ip 和 真实客户端真实 ip ，建议使用</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">log_format combined_realip <span class="string">'$proxy_add_x_forwarded_for - $remote_user [$time_local]'</span></div><div class="line"><span class="string">'"$request" $status $body_byres_sent'</span></div><div class="line"><span class="string">'"$http_referer" "$http_user_agent"'</span>;</div></pre></td></tr></table></figure>
<h2 id="2-错误日志-error-log-日志级别"><a href="#2-错误日志-error-log-日志级别" class="headerlink" title="2.错误日志 error_log 日志级别"></a>2.错误日志 error_log 日志级别</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在常见的502中已经提到过这个概念。error_log 级别分为 debug，info，notice，warn，error，crit 默认为 crit ，该级别在日志名后边定义格式如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">error_log /usr/<span class="built_in">local</span>/nginx/logs/nginx_error.log crit;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nginx%20%E4%B8%8D%E8%AE%B0%E5%BD%95%E6%8C%87%E5%AE%9A%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%97%A5%E5%BF%97/03.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;crit 记录的日志最少，而 debug 记录的日志最多。如果 nginx 遇到一些问题，比如 502 比较频繁出现，但是看默认的 error_log 并没有看到有意义的信息，那么就可以调一下错误日志的级别，当调成 error 级别时，错误日志记录的内容会更加丰富。    </p>
<h2 id="3-某些类型的文件不记录日志"><a href="#3-某些类型的文件不记录日志" class="headerlink" title="3.某些类型的文件不记录日志"></a>3.某些类型的文件不记录日志</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改虚拟主机配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@lnmp ~]<span class="comment"># vim /usr/local/nginx/conf/vhosts/123.conf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置内容：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$</div><div class="line"> &#123;</div><div class="line"> access.log off;</div><div class="line"> &#125;</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/nginx%20%E4%B8%8D%E8%AE%B0%E5%BD%95%E6%8C%87%E5%AE%9A%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%97%A5%E5%BF%97/04.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样就不会记录以上类型的日志，其实 static 和cache 的相关日志也可以禁止</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Nginx/17. nginx 不记录指定文件类型的日志/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
